package com.systop.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.systop.constant.AuthServiceConstant;
import com.systop.domain.to.SucLoginUserTo;
import com.systop.domain.vo.ConsigneeVo;
import com.systop.entity.Consignee;
import com.systop.entity.SysUser;
import com.systop.service.ConsigneeService;
import com.systop.service.SysUserService;
import com.systop.utils.R;
import com.systop.utils.RedisUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/MemberInfo")
public class ConsigneeController {
    @Autowired
    private ConsigneeService consigneeService;

    @Autowired
    SysUserService sysUserService;

    //查询所有收货地址
    @RequestMapping(value = "/getConsigneeByUserCode",method = RequestMethod.POST)
    @ResponseBody
    public R getConsigneeByUserCode(@RequestBody SucLoginUserTo sucLoginUserTo){
        System.out.println(sucLoginUserTo);

        if (sucLoginUserTo==null) {
            return R.error("用户不存在");
        }
        //获取用户信息
        SysUser user = sysUserService.getOne(
                new QueryWrapper<SysUser>().
                        eq("user_code", sucLoginUserTo.getUserCode()));

        List<Consignee> address = consigneeService.list(new QueryWrapper<Consignee>().eq("user_id", user.getUserId()));
        return R.ok().put("address",address);
    }

    @RequestMapping(value = "/showConsigneeInfo",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> showConsigneeInfo(@RequestParam Map<String,Object> map){
        //输出返回值数据
        System.out.println(map);
        // List<Consignee> list = JSON.parseObject((byte[]) map.get("consignee"),Consignee.class);
        //将JSON数据转换成java能识别的对象类数据
        Consignee consignee =
                JSON.parseObject(JSON.parse(JSON.toJSONString(map.get("consignee"))).toString(),
                        new TypeReference<Consignee>(){});
            //输出转化后数据
        System.out.println(consignee);
        if(!consignee.getSysUser().getUserCode().equals("")){
            Long id = consigneeService.findIdByUserCode(consignee.getSysUser().getUserCode());
            consignee.setUserId(id);
        }
        //返回添加修改下拉框数据
        List<SysUser> sysUserList = consigneeService.findAllSysUserName();
        //返回查询数据总条数
        int pageCount = consigneeService.countAllConsignee(consignee);
        map.put("pageCount",pageCount);
        //分页查询用索引
        consignee.getSysUser().setStart((consignee.getSysUser().getStart()-1)*consignee.getSysUser().getPageSize());
        //返回页面查询数据
        List<Consignee> list = consigneeService.showConsigneeInfo(consignee);
        //处理当前页面空值
        if(CollectionUtils.isEmpty(list) && consignee.getSysUser().getStart()>0){
            //设置当本页没有数据时返回上一页
            consignee.getSysUser().setStart(consignee.getSysUser().getStart()/consignee.getSysUser().getPageSize());
            map.put("consignee", consignee);
            return showConsigneeInfo(map);
        }
        map.put("sysuserList",sysUserList);
        map.put("userlist", list);
        return map;
    }

    //删除收货人信息
    @RequestMapping(value = "/delConsignee",method = RequestMethod.POST)
    @ResponseBody
    public R delConsignee(@RequestBody Consignee consignee){
        System.out.println(consignee);
        // Map<String,Object> map = new HashMap<>();
        R r = new R();
        //如果存在相应订单则无法删除相应收货人
        int exist = consigneeService.findOrder(consignee.getConsigneeId());
        if(exist==0){
            int i = consigneeService.delConsignee(consignee.getConsigneeId());
            // r.put("delSuccNum",i);
            if(i<=0){
                return r.error("删除失败,用户不存在");
            }
        }else{
            return r.error("删除失败,订单中存在相应收货人");
        }
        return r.ok("删除成功!");
    };

    //添加收货相关信息
    @RequestMapping(value = "/addConsignee",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> addConsignee(@RequestBody Map<String,Object> map){
        System.out.println(map);
        //将JSON数据转换成java能识别的对象类数据
        Consignee consignee =
                JSON.parseObject(JSON.parse(JSON.toJSONString(map.get("consignee"))).toString(),
                        new TypeReference<Consignee>(){});
        System.out.println(consignee);
        int i = consigneeService.addConsignee(consignee);
        map.put("udpSuccNum",i);
        if(i>0){
            map.put("msg", "添加成功!");
        }else{
            map.put("msg","添加失败,用户不存在");
        }
        return map;
    }

    //修改收货人相关信息
    @RequestMapping(value = "/updConsignee",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> updConsignee(@RequestBody Consignee consignee){
        System.out.println(consignee);
        Map<String,Object> map = new HashMap<>();
        int i = consigneeService.updConsignee(consignee);
        map.put("udpSuccNum",i);
        if(i>0){
            map.put("msg", "修改成功!");
        }else{
            map.put("msg","修改失败,数据错误");
        }
        return map;
    };

    //修改收货人相关信息
    @RequestMapping(value = "/address/addConsignee",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> addConsignee(@RequestBody ConsigneeVo consigneeVo){

        System.out.println(consigneeVo);
        R r = new R();

        if (consigneeVo == null) {
            return r.error();
        }

        //判断用户是否存在
        SysUser user = sysUserService.findUserByUserCode(consigneeVo.getUserCode());
        if (user == null){
            return r.error("用户不存在");
        }

        //填充添加信息
        Consignee consignee = new Consignee();
        BeanUtils.copyProperties(consigneeVo, consignee);
        consignee.setSysUser(user);

        int i = consigneeService.addConsignee(consignee);
        if(i==0){
            return r.error("添加失败,数据错误");
        }
        return r.ok("添加成功!");
    };
}
